var util = require('../../utils/util.js')
var app = getApp()
Page({
  data: {
    userInfo: [],
    liuyancontent:'',
    maxlength:80,
    realength:0
  },
  onLoad: function () {
    var that = this
    console.log(app.globalData.desk_code);
    // app.getUserInfo(function (userInfo) {
      //加载完更新数据  
      that.setData({
        userInfo: app.globalData.userInfo
      })
      // console.log(this.data.userInfo)
      // that.update()
    // })
  },
  //下拉刷新
  // onPullDownRefresh: function () {
  //   var that = this;
  //   that.onLoad(that.data.dgGlobal_options);
  //   setTimeout(() => {
  //     wx.stopPullDownRefresh();
  //   }, 1000);  
  // },
  onShareAppMessage: function () {
    var that = this;
    var shareTitle = '留言板';
    var shareDesc = '留言板小程序';
    var sharePath = 'pages/liuyan/liuyan';
    return {
      title: shareTitle,
      desc: shareDesc,
      imageUrl: '',
      path: sharePath
    }
  },
  bindTextAreaBlur: function (e) { //失去焦点触发
    this.setData({ liuyancontent: e.detail.value });
  },
  bindTextinput : function (e) {  //输入触发
    this.setData({ 
       liuyancontent: e.detail.value,
       realength    : e.detail.value.length
       
    });
  },
  
  setliuyan:function(){
    if (!this.data.liuyancontent){
      wx.showToast({
          title: '请输入留言内容',
          icon: 'success',
          duration: 1500
      })
      return;  
    }
    var that = this.data
    wx.showModal({
      title: '是否确认提交',
      content: '',
      success: function (res) {
        if (res.confirm) {
          //提交留言
          wx.request({
            url: 'https://liuyan.ijiechen.com/front/add-msg/', //
            // method: 'POST',
            data: {
              username     : that.userInfo.nickName,
              useravatar   : that.userInfo.avatarUrl,
              liuyancontent: that.liuyancontent,
              desk_code:app.globalData.desk_code,          
            
            },
            header: {
              'content-type': 'application/json' // 
              // 'content-type': 'application/x-www-form-urlencoded' // post
            },
            success: function (res) {
              console.log(res.data)
             
              wx.showModal({
                title: '提交成功',
                content: '点击确认返回留言页面',
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')                    
                    wx.navigateBack({
                      delta: 1
                    })
                  }
                }
              })
            }
          })

        }
      }
    })  
  }
})
